package com.joyoung.common.utils.singn;

public class XXTea {
    public XXTea() {
    }
    
    public static void main(String[] args) {
        try {
            String userName = "15213603672";
            String passwd = "111111";
            String xxteaKey = Encrypt.MD5(userName + passwd).substring(0, 16);
            String sessionId = "5cbb51269a5843c29bfa517e450492d8";
            String sessionKey = Encrypt.MD5(sessionId);
            System.out.println("sessionKey=" + sessionKey);
            String accessToken = "243fe68343a7193c86ad1cd14d835021cc9b5975aff48ee175f4a75a8da65281abfc830b";
            System.out.println("accessToken=" + accessToken);
            byte[] accessToken2 = Hex.decodeHex(accessToken.toCharArray());
            String sessionKey2 = new String(decrypt(accessToken2, xxteaKey.getBytes()));
            System.out.println("解密后的sessionKey=" + sessionKey2);
            String dataKey = "";
            
            for(int i = 0; i < sessionKey2.length(); ++i) {
                if (i % 2 == 0) {
                    dataKey = dataKey + sessionKey2.substring(i, i + 1);
                }
            }
            
            System.out.println("dataKey=" + dataKey);
        } catch (Exception var11) {
            var11.printStackTrace();
        }
        
    }
    
    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        return data.length == 0 ? data : toByteArray(encrypt(toIntArray(data, true), toIntArray(key, false)), false);
    }
    
    public static byte[] decrypt(byte[] data, byte[] key) {
        return data.length == 0 ? data : toByteArray(decrypt(toIntArray(data, false), toIntArray(key, false)), true);
    }
    
    private static int[] encrypt(int[] v, int[] k) {
        int n = v.length - 1;
        if (n < 1) {
            return v;
        } else {
            if (k.length < 4) {
                int[] key = new int[4];
                System.arraycopy(k, 0, key, 0, k.length);
                k = key;
            }
            
            int z = v[n];
            int y = v[0];
            int delta = -1640531527;
            int sum = 0;
            
            int e;
            int p;
            for(int var9 = 6 + 52 / (n + 1); var9-- > 0; z = v[n] += (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z)) {
                sum += delta;
                e = sum >>> 2 & 3;
                
                for(p = 0; p < n; ++p) {
                    y = v[p + 1];
                    z = v[p] += (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                }
                
                y = v[0];
            }
            
            return v;
        }
    }
    
    private static int[] decrypt(int[] v, int[] k) {
        int n = v.length - 1;
        if (n < 1) {
            return v;
        } else {
            if (k.length < 4) {
                int[] key = new int[4];
                System.arraycopy(k, 0, key, 0, k.length);
                k = key;
            }
            
            int var10000 = v[n];
            int y = v[0];
            int delta = -1640531527;
            int q = 6 + 52 / (n + 1);
            
            for(int sum = q * delta; sum != 0; sum -= delta) {
                int e = sum >>> 2 & 3;
                
                int p;
                int z;
                for(p = n; p > 0; --p) {
                    z = v[p - 1];
                    y = v[p] -= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
                }
                
                z = v[n];
                y = v[0] -= (z >>> 5 ^ y << 2) + (y >>> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z);
            }
            
            return v;
        }
    }
    
    private static int[] toIntArray(byte[] data, boolean includeLength) {
        int n = (data.length & 3) == 0 ? data.length >>> 2 : (data.length >>> 2) + 1;
        int[] result;
        if (includeLength) {
            result = new int[n + 1];
            result[n] = data.length;
        } else {
            result = new int[n];
        }
        
        n = data.length;
        
        for(int i = 0; i < n; ++i) {
            result[i >>> 2] |= (255 & data[i]) << ((i & 3) << 3);
        }
        
        return result;
    }
    
    private static byte[] toByteArray(int[] data, boolean includeLength) {
        int n;
        if (includeLength) {
            n = data[data.length - 1];
        } else {
            n = data.length << 2;
        }
        
        byte[] result = new byte[n];
        
        for(int i = 0; i < n; ++i) {
            result[i] = (byte)(data[i >>> 2] >>> ((i & 3) << 3));
        }
        
        return result;
    }
}
